package com.happystreet.engine
{
	import com.happystreet.ui.ToolTip;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Matrix;
	
	public class CoreObject extends Sprite
	{
		public static const ALIGN_TOP:uint = 1;
		public static const ALIGN_RIGHT:uint = 2;
		public static const ALIGN_BOTTOM:uint = 4;
		public static const ALIGN_LEFT:uint = 8;
		public static const ALIGN_HORIZONTAL_CENTER:uint = 16;
		public static const ALIGN_VERTICAL_CENTER:uint = 32;
		public static const ALIGN_CENTER:uint = (ALIGN_HORIZONTAL_CENTER | ALIGN_VERTICAL_CENTER);
		public static const FLIP_RESET:uint = 0;
		public static const FLIP_HORIZONTAL:uint = 1;
		public static const FLIP_VERTICAL:uint = 2;
		
		public var tooltip:ToolTip;
		public var updateList:Array;
		public var frameSkip:int;
		private var loopsTotal:int;
		public var tooltipHitbox:Sprite;
		public var tooltipEnabled:Boolean;
		private var loopNum:int;
		private var removeAtLastLoop:Boolean;
		public var drawingPriority:int;
		public var content:MovieClip;
		/**
		 *核心对象 
		 * @param className		类名
		 * @param content		实体
		 * 
		 */		
		public function CoreObject(className:String=null,content:MovieClip=null)
		{
			super();
			this.updateList = new Array();
			this.drawingPriority = 0;
			this.tabEnabled = false;
			
			if(className!=null){
				this.content = GameEngine.createMovieClip(className);
				if(this.content!=null){
					this.addChild(this.content);
					this.frameSkip = 1;
					this.content.gotoAndStop(1);
				}
			}else{
				if(content!=null){
					this.content= content;
					this.addChild(this.content);
					this.frameSkip = 1;
					this.content.gotoAndStop(1);
				}
			}
		}
		
		/**
		 *设置缩放 
		 * @param value
		 * 
		 */		
		public function set scale(value:Number):void
		{
			this.scaleX = this.scaleY = value;
		}
		/**
		 *添加coreObj到显示列表汇总 
		 * @param obj
		 * 
		 */		
		public function add(obj:CoreObject):void
		{
			var cobj:CoreObject;
			if(obj==null){
				return;
			}
			
			if(!(obj.parent==null)&&(obj.parent is CoreObject)){
				CoreObject(obj.parent).removeChild(obj);
			}
			
			var upDate:Boolean;
			var index:int;
			var listLen:int = this.updateList.length;
			while (index < listLen)
			{
				cobj = this.updateList[index];
				if (obj.drawingPriority < cobj.drawingPriority)
				{
					this.updateList.splice(index, 0, obj);
					addChildAt(obj, getChildIndex(cobj));
					upDate = true;
					break;
				}
				index++;
			}
			if (!upDate)
			{
				this.updateList.push(obj);
				addChild(obj);
			}
		}
		
		public function remove(obj:CoreObject):void
		{
			
		}
		
		public function flip(_arg1:uint):void
		{
			var _local2:Matrix;
			if (_arg1 == 0)
			{
				_local2 = this.transform.matrix;
				_local2.a = 1;
				_local2.d = 1;
				this.transform.matrix = _local2;
			}
			else
			{
				if (_arg1 == FLIP_HORIZONTAL)
				{
					if (scaleX > 0)
					{
						scaleX = -(scaleX);
					};
				}
				else
				{
					if (_arg1 == FLIP_VERTICAL)
					{
						if (scaleY > 0)
						{
							scaleY = -(scaleY);
						}
					}
				}
			}
		}
		
		public function playContent(_arg1:int=0, _arg2:Boolean=false, _arg3:Boolean=false):void
		{
			this.loopsTotal = _arg1;
			this.removeAtLastLoop = _arg2;
			var _local4:int = ((_arg3) ? Math.floor((Math.random() * this.content.totalFrames)) : 1);
			if (this.content.totalFrames > 1)
			{
				this.content.gotoAndPlay(_local4);
				if (this.loopsTotal > 0)
				{
					this.loopNum = 0;
					if (!this.hasEventListener(Event.ENTER_FRAME))
					{
						this.addEventListener(Event.ENTER_FRAME, this.checkLoop, false, 0, true);
					}
				}
			}
		}
		
		public function stopContent():void
		{
			if (this.hasEventListener(Event.ENTER_FRAME))
			{
				this.removeEventListener(Event.ENTER_FRAME, this.forwardFrame);
			}
			this.content.gotoAndStop(1);
		}
		
		private function forwardFrame(evt:Event):void
		{
			var next_frame:int = (this.content.currentFrame + this.frameSkip);
			if (next_frame > this.content.totalFrames)
			{
				if (this.loopsTotal)
				{
					this.loopNum++;
					if (this.loopNum == this.loopsTotal)
					{
						if (this.removeAtLastLoop)
						{
							try
							{
								(parent as CoreObject).remove(this);
							}
							catch(e:Error)
							{
								trace(e.message+" > Error while removing "+this.name);
							}
						}
						else
						{
							this.content.gotoAndStop((this.content.totalFrames - 1));
						};
						this.removeEventListener(Event.ENTER_FRAME, this.forwardFrame);
						return;
					}
				}
				next_frame = (next_frame - this.content.totalFrames);
			}
			this.content.gotoAndStop(next_frame);
		}
		
		private function checkLoop(evt:Event):void
		{
			if (this.loopsTotal)
			{
				if (this.content.currentFrame == this.content.totalFrames)
				{
					this.loopNum++;
					if (this.loopNum == this.loopsTotal)
					{
						if (this.removeAtLastLoop)
						{
							try
							{
								(parent as CoreObject).remove(this);
							}
							catch(e:Error)
							{
								trace(e.message+" > Error while removing "+this.name);
							}
						}
						else
						{
							this.content.gotoAndStop((this.content.totalFrames - 1));
						}
						this.removeEventListener(Event.ENTER_FRAME, this.checkLoop);
						return;
					}
				}
			}
		}
		
		
		/**
		 *对齐 
		 * @param wNum
		 * @param hNum
		 * @param _arg3
		 * 
		 */		
		public function alignTo(wNum:Number, hNum:Number, align:uint=0):void
		{
			if ((align & ALIGN_RIGHT) != 0)
			{
				x = (wNum - width);
			}
			else
			{
				if ((align & ALIGN_HORIZONTAL_CENTER) != 0)
				{
					x = (wNum - (width / 2));
				}
				else
				{
					x = wNum;
				}
			}
			if ((align & ALIGN_BOTTOM) != 0)
			{
				y = (hNum - height);
			}
			else
			{
				if ((align & ALIGN_VERTICAL_CENTER) != 0)
				{
					y = (hNum - (height / 2));
				}
				else
				{
					y = hNum;
				}
			}
		}
		
		public function setOptimizedObject(_arg1:Boolean=false):void
		{
			if (this.content)
			{
				if (this.content.totalFrames > 1)
				{
					if (_arg1)
					{
						this.content.stop();
						this.cacheAsBitmap = true;
					}
				}
				else
				{
					this.cacheAsBitmap = true;
				}
			}
		}
		
		public function createTooltip(_arg1:String, _arg2:int=0, _arg3:int=0, _arg4:Boolean=false, _arg5:Boolean=false, _arg6:Boolean=false):void
		{

		}
	}
}